package com.sm.auth.controller;

import com.sm.auth.dto.LoginDto;
import com.sm.auth.service.AuthService;
import com.sm.auth.service.AuthService;
import com.sm.auth.utils.SecurityUtil;
import com.sm.auth.vo.LoginUserVo;
import com.sm.common.constants.HttpStatus;
import com.sm.common.response.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("auth")
@Slf4j
public class AuthController {

    @Autowired
    private AuthService authService;
    @Autowired
    private PasswordEncoder passwordEncoder;

    @PostMapping("sys")
    public Result login(@RequestBody LoginDto loginDto) {
        String token = authService.login(loginDto);
        return new Result(HttpStatus.SUCCESS,"登陆成功！！！",token);
    }
    @GetMapping("test")
    @PreAuthorize("hasAuthority('system:user:list')")
    public Result test(){
        String passwordSecret = passwordEncoder.encode("123456");
        System.out.println(passwordSecret);
        return Result.success().put("data",passwordSecret);
    }
    @GetMapping("getCurrentUser")
    public Result getCurrentUser(){
        LoginUserVo currentUser = SecurityUtil.getCurrentUser();
        return Result.success().put("data", currentUser);
    }
}
